//
//  AddViewController.m
//  AddressBook
//
//  Created by 解晓东 on 15/11/7.
//  Copyright (c) 2015年 sasa. All rights reserved.
//

#import "AddViewController.h"
#import "SAContactModel.h"

@interface AddViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

- (IBAction)backAction:(id)sender;
- (IBAction)addAction;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneField];
}

#pragma mark - 修改按钮的状态
- (void)textChange {
    // 用户名和密码都不为空，按钮才能点击
    /*
     if (self.nameField.text.length && self.pwdField.text.length) {
     self.loginBtn.enabled = YES;
     } else {
     self.loginBtn.enabled = NO;
     }
     */
    
    self.addBtn.enabled = (self.nameField.text.length && self.phoneField.text.length);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // 让姓名文本框成为第一响应者（叫出键盘）
    [self.nameField becomeFirstResponder];
}

#pragma mark - 添加数据
- (IBAction)backAction:(id)sender {
    // 1.关闭当前视图控制器
    [self.navigationController popViewControllerAnimated:YES];
    // 代理传值
    if ([self.delegate respondsToSelector:@selector(addContact:didAddContact:)]) {
        SAContactModel *contactModel = [[SAContactModel alloc] init];
        contactModel.name = self.nameField.text;
        contactModel.phone = self.phoneField.text;
        [self.delegate addContact:self didAddContact:contactModel];
    }
}

- (IBAction)addAction {
}
@end
